home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / qud_cksm.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  5KB  |  134 lines

  1. /* lib/des/qud_cksm.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. /* From "Message Authentication"  R.R. Jueneman, S.M. Matyas, C.H. Meyer
  49.  * IEEE Communications Magazine Sept 1985 Vol. 23 No. 9 p 29-40
  50.  * This module in only based on the code in this paper and is
  51.  * almost definitely not the same as the MIT implementation.
  52.  */
  53. #include "des_locl.h"
  54.  
  55. /* bug fix for dos - 7/6/91 - Larry hughes@logos.ucs.indiana.edu */
  56. #define B0(a)    (((unsigned long)(a)))
  57. #define B1(a)    (((unsigned long)(a))<<8)
  58. #define B2(a)    (((unsigned long)(a))<<16)
  59. #define B3(a)    (((unsigned long)(a))<<24)
  60.  
  61. /* used to scramble things a bit */
  62. /* Got the value MIT uses via brute force :-) 2/10/90 eay */
  63. #define NOISE    ((unsigned long)83653421L)
  64.  
  65. unsigned long des_quad_cksum(input, output, length, out_count, seed)
  66. des_cblock (*input);
  67. des_cblock (*output);
  68. long length;
  69. int out_count;
  70. des_cblock (*seed);
  71.     {
  72.     unsigned long z0,z1,t0,t1;
  73.     int i;
  74.     long l=0;
  75.     unsigned char *cp;
  76.     unsigned char *lp;
  77.  
  78.     if (out_count < 1) out_count=1;
  79.     lp=(unsigned char *)output;
  80.  
  81.     z0=B0((*seed)[0])|B1((*seed)[1])|B2((*seed)[2])|B3((*seed)[3]);
  82.     z1=B0((*seed)[4])|B1((*seed)[5])|B2((*seed)[6])|B3((*seed)[7]);
  83.  
  84.     for (i=0; ((i<4)&&(i<out_count)); i++)
  85.         {
  86.         cp=(unsigned char *)input;
  87.         l=length;
  88.         while (l > 0)
  89.             {
  90.             if (l > 1)
  91.                 {
  92.                 t0= (unsigned long)(*(cp++));
  93.                 t0|=(unsigned long)B1(*(cp++));
  94.                 l--;
  95.                 }
  96.             else
  97.                 t0= (unsigned long)(*(cp++));
  98.             l--;
  99.             /* add */
  100.             t0+=z0;
  101.             t0&=0xffffffffL;
  102.             t1=z1;
  103.             /* square, well sort of square */
  104.             z0=((((t0*t0)&0xffffffffL)+((t1*t1)&0xffffffffL))
  105.                 &0xffffffffL)%0x7fffffffL; 
  106.             z1=((t0*((t1+NOISE)&0xffffffffL))&0xffffffffL)%0x7fffffffL;
  107.             }
  108.         if (lp != NULL)
  109.             {
  110.             /* I believe I finally have things worked out.
  111.              * The MIT library assumes that the checksum
  112.              * is one huge number and it is returned in a
  113.              * host dependant byte order.
  114.              */
  115.             static unsigned long ltmp=1;
  116.             static unsigned char *c=(unsigned char *)<mp;
  117.  
  118.             if (c[0])
  119.                 {
  120.                 l2c(z0,lp);
  121.                 l2c(z1,lp);
  122.                 }
  123.             else
  124.                 {
  125.                 lp=output[out_count-i-1];
  126.                 l2n(z1,lp);
  127.                 l2n(z0,lp);
  128.                 }
  129.             }
  130.         }
  131.     return(z0);
  132.     }
  133.  
  134.